<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="操作类型" prop="operType">
        <el-select v-model="queryParams.operType" placeholder="请选择操作类型" clearable size="small">
          <el-option label="结果提交" value="标注:结果提交" />
          <el-option label="数据同步" value="标注:数据同步" />
        </el-select>
      </el-form-item>
      <el-form-item label="一级目录名" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入一级目录名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次名" prop="batchName">
        <el-input
          v-model="queryParams.batchName"
          placeholder="请输入批次名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包名" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入包名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标注员" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入标注员名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录id" prop="mQaResultId">
        <el-input
          v-model="queryParams.mQaResultId"
          placeholder="请输入记录id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mark:markLog:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['mark:markLog:edit']"
          >恢复</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="markLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作类型" align="center" prop="operType" />
      <el-table-column label="一级目录名" align="center" prop="taskName" />
      <el-table-column label="批次名" align="center" prop="batchName" />
      <el-table-column label="包名" align="center" prop="packageName" />
      <el-table-column label="文件名" align="center" prop="fileName" />
      <el-table-column label="标注结果记录id" align="center" prop="mQaResultId" />
      <el-table-column label="二维框数" align="center" prop="statistic1" />
      <el-table-column label="三维框数" align="center" prop="statistic2" />
      <el-table-column label="标注员" align="center" prop="createBy" />
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.operType == '标注:结果提交' || scope.row.operType == '验收:结果提交'"
            v-hasPermi="['mark:markLog:edit']"
            >恢复</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mark:markLog:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改标注操作记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="操作类型">
          <el-select v-model="form.operType" placeholder="请选择操作类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="一级目录名" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入一级目录名" />
        </el-form-item>
        <el-form-item label="批次名" prop="batchName">
          <el-input v-model="form.batchName" placeholder="请输入批次名" />
        </el-form-item>
        <el-form-item label="包名" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入包名" />
        </el-form-item>
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名" />
        </el-form-item>
        <el-form-item label="标注结果记录id" prop="mQaResultId">
          <el-input v-model="form.mQaResultId" placeholder="请输入标注结果记录id" />
        </el-form-item>
        <el-form-item label="统计信息1" prop="statistic1">
          <el-input v-model="form.statistic1" placeholder="请输入统计信息1" />
        </el-form-item>
        <el-form-item label="统计信息2" prop="statistic2">
          <el-input v-model="form.statistic2" placeholder="请输入统计信息2" />
        </el-form-item>
        <el-form-item label="统计信息3" prop="statistic3">
          <el-input v-model="form.statistic3" placeholder="请输入统计信息3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMarkLog,
  getMarkLog,
  restoreMarkLog,
  delMarkLog,
  addMarkLog,
  updateMarkLog,
  exportMarkLog,
} from '@/api/mark/markLog'

export default {
  name: 'MarkLog',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 标注操作记录表格数据
      markLogList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operType: undefined,
        taskName: undefined,
        batchName: undefined,
        packageName: undefined,
        fileName: undefined,
        mQaResultId: undefined,
        createBy: undefined,
        jsonstr: undefined,
        statistic1: undefined,
        statistic2: undefined,
        statistic3: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [{ required: true, message: '一级目录名不能为空', trigger: 'blur' }],
        batchName: [{ required: true, message: '批次名不能为空', trigger: 'blur' }],
        mQaResultId: [{ required: true, message: '标注结果记录id不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询标注操作记录列表 */
    getList() {
      this.loading = true
      listMarkLog(this.queryParams).then((response) => {
        this.markLogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        markLogId: undefined,
        operType: undefined,
        taskName: undefined,
        batchName: undefined,
        packageName: undefined,
        fileName: undefined,
        mQaResultId: undefined,
        jsonstr: undefined,
        createBy: undefined,
        createTime: undefined,
        statistic1: undefined,
        statistic2: undefined,
        statistic3: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.markLogId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加标注操作记录'
    },
    /** 恢复按钮操作 */
    handleUpdate(row) {
      this.reset()
      const markLogIds = row.markLogId || this.ids
      this.$confirm('恢复选中的标注结果?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          restoreMarkLog(markLogIds).then((response) => {
            if (response.code === 200) {
              this.msgSuccess('恢复成功，请在5分钟后刷新验收页面检查标注结果。')
              this.open = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        })
        .catch(() => {})
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.markLogId != undefined) {
            updateMarkLog(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addMarkLog(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const markLogIds = row.markLogId || this.ids
      this.$confirm('是否确认删除标注操作记录编号为"' + markLogIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delMarkLog(markLogIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有标注操作记录数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportMarkLog(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
  },
}
</script>
